/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/8  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.mapper;

import com.sansi.pencilbook.entity.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 订单的增删改查
 *
 * @author lisy31662
 */
@Mapper
public interface OrderMapper {

  @Insert("insert into order (serial_no, store_id, isbn, user_id, book_price, tran_number, amount, tran_time, address_id) " +
      "values(#{serialNo},#{storeId},#{isbn},#{userId},#{bookPrice},#{tranNumber},#{amount},#{tranTime},#{addressId})")
  public int saveOrder(Order order) throws SQLException;

  @Insert("<script>" +
      "insert into order (serial_no, store_id, isbn, user_id, book_price, tran_number, amount, tran_time, address_id) " +
      "values " +
      "<foreach collection='orders' item='order' index='index' separator=','>" +
      "(#{order.serialNo},#{order.storeId},#{order.isbn},#{order.userId},#{order.bookPrice},#{order.tranNumber}," +
      "#{order.amount},#{order.tranTime},#{order.addressId})" +
      "</foreach>" +
      "</script>")
  public int saveOrders(List<Order> orders) throws SQLException;

  @Select("select serial_no, store_id, isbn, user_id, book_price, tran_number, amount, tran_time, address_id from order")
  public List<Order> findOrdersAll() throws SQLException;

  @Select("select serial_no, store_id, isbn, user_id, book_price, tran_number, amount, tran_time, address_id " +
      "from order where serial_no=#{serialNo}")
  public List<Order> findOrdersBySerial(String serialNo) throws SQLException;

  @Select("select serial_no, store_id, isbn, user_id, book_price, tran_number, amount, tran_time, address_id " +
      "from order where user_id=#{userId}")
  public List<Order> findOrdersByUser(int userId) throws SQLException;

  @Select("select serial_no, store_id, isbn, user_id, book_price, tran_number, amount, tran_time, address_id " +
      "from order where store_id=#{storeId}")
  public List<Order> findOrdersByStore(int storeId) throws SQLException;

  @Update("update order set book_price=#{bookPrice}, tran_number=#{tranNumber}, " +
      "amount=#{amount}, tran_time=#{tranTime}, address_id=#{addressId} " +
      "where serial_no=#{serialNo} and store_id=#{storeId} and isbn=#{isbn} and user_id=#{userId}")
  public int updateOrder(Order order) throws SQLException;

}

